#include "Calendar.h"

//Default Constructor
Calendar::Calendar(){
this->days = new Day[7];
}
//Destructor
Calendar::~Calendar(){
	delete[] days;
}
//Operator '=' Overloading
const Calendar& Calendar::operator=(const Calendar& c){
	if (this!=&c) { 
		this->assign(c);
	}
	return *this;
}

// A function to determine if a given daye is legal
const bool Calendar::legalDate(int day,float start,float end) {
	bool b =  day<=7 && day>=1 && start<=end &&start >=0.0 && start<24.0 && end<24.0 && end>0.0;
	if (!b)
		 cout<<"Ilegal meeting parameters"<<endl;
	return b;
}

//false for failure, true for success
//No const for m - We are modifying it !
const bool Calendar::addMeeting(int day,  Meeting& m) {
	bool success = true;
	success=legalDate(day,m.getStartHour(),m.getEndHour());
	if (!success) {
		cout<<"Failed to add a meeting"<<endl;
		return false;
	}
	else{
		days[day-1].addMeeting(m);
	}
	return success;
}

//Deleting a meeting
const bool Calendar:: deleteMeeting(int day_to_del,const Meeting& m){
	bool delSuccess = true;
	if (!legalDate(day_to_del,m.getStartHour(),m.getEndHour()))
	{
		cout<<"Ilegal Date and/or time"<<endl;
		return false;
	}
	else {
		days[day_to_del-1].delMeeting(m);
	}
	return delSuccess;
}
//Finds a given meeting
const bool Calendar::findMeeting(int day, float start)  {
	if (this->legalDate(day,start,23.999))
	{
		Meeting* found =  (days[day-1].findMeeting(start));
		if (found!=NULL)
			found->printMeeting();
	}
	else {
		cout<<"Ilegal Date and/or Time"<<endl;
		return false;
	}
}

//Copies a meeting from day to day
void Calendar::copyMeeting(int source_day,int dest_day,const Meeting& m_source, Meeting& m_dest){
	if (legalDate(source_day,m_source.getStartHour(),23.9999) && legalDate(dest_day,m_dest.getStartHour(),23.9999)) {

		Meeting* m = days[source_day-1].findMeeting(m_source.getStartHour());
		if (m!=NULL){
			//I found the meeting, now I check whther there is an overlap in the destinated day and time
			float dest_end_hour = m_dest.getStartHour()+ (m->getEndHour()-m->getStartHour());
			if (legalDate(dest_day,m_dest.getStartHour(),dest_end_hour)){
			m_dest.setEndHour(dest_end_hour);	
			if (days[dest_day-1].addMeeting(m_dest) == true)  {
				cout<<"Meeting copied successfuly"<<endl;
				return;
			}
			}
			else  {
				cout<<"Meeting copy failed due to an overlap in date and time/bad destination meeting hour"<<endl;
				return;
			}
		}
	}

}
//A virtual function - prints meeting
void Calendar::printMeetings() {
	for(int i=0; i<7; i++) {

		cout<<endl<<"Meetings on day "<<i+1<<":"<<endl;

		this->days[i].printMeetings();
	}

}

//A function to use with the operator '=' overloading
const Calendar& Calendar::assign(const Calendar& c){
	Day* new_days = new Day[7];
	for(int i=0;i<7;i++)
		new_days[i] = c.days[i];
	delete[] this->days;
	this->days = new_days;
	return *this;
}